package cn.edu.nju.software.activity;

import cn.edu.nju.activity.R;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.app.Activity;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		setListener();
		iniContent();
	}
	
	private void clearMenu() {
		
		ImageButton home = (ImageButton) findViewById(R.id.home);
		home.setBackgroundColor(getResources().getColor(R.color.theme));
		
		ImageButton user = (ImageButton) findViewById(R.id.user);
		user.setBackgroundColor(getResources().getColor(R.color.theme));
		
		ImageButton message = (ImageButton) findViewById(R.id.message);
		message.setBackgroundColor(getResources().getColor(R.color.theme));
		
		ImageButton friends = (ImageButton) findViewById(R.id.friends);
		friends.setBackgroundColor(getResources().getColor(R.color.theme));
		
		ImageButton talk = (ImageButton) findViewById(R.id.talk);
		talk.setBackgroundColor(getResources().getColor(R.color.theme));
		
		ImageButton settings = (ImageButton) findViewById(R.id.settings);
		settings.setBackgroundColor(getResources().getColor(R.color.theme));
		
	}

	private void setListener() {

		ImageButton news = (ImageButton) findViewById(R.id.news);

		news.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){   
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}else if(event.getAction() == MotionEvent.ACTION_UP){     
					v.setBackgroundColor(getResources().getColor(R.color.theme));
				}
				return false;     
			}
		});

		ImageButton photo = (ImageButton) findViewById(R.id.photo);

		photo.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){   
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}else if(event.getAction() == MotionEvent.ACTION_UP){     
					v.setBackgroundColor(getResources().getColor(R.color.theme));
				}
				return false;     
			}
		});

		ImageButton refresh = (ImageButton) findViewById(R.id.refresh);

		refresh.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){   
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}else if(event.getAction() == MotionEvent.ACTION_UP){     
					v.setBackgroundColor(getResources().getColor(R.color.theme));
				}
				return false;     
			}
		});

		ImageButton home = (ImageButton) findViewById(R.id.home);

		home.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					clearMenu();
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}
				return false;     
			}
		});

		ImageButton user = (ImageButton) findViewById(R.id.user);

		user.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					clearMenu();
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}
				return false;     
			}
		});

		ImageButton message = (ImageButton) findViewById(R.id.message);

		message.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					clearMenu();
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}
				return false;     
			}
		});

		ImageButton friends = (ImageButton) findViewById(R.id.friends);

		friends.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					clearMenu();
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}
				return false;     
			}
		});

		ImageButton talk = (ImageButton) findViewById(R.id.talk);

		talk.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					clearMenu();
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}
				return false;
			}
		});

		ImageButton settings = (ImageButton) findViewById(R.id.settings);

		settings.setOnTouchListener(new OnTouchListener(){     
			@Override    
			public boolean onTouch(View v, MotionEvent event) {     
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					clearMenu();
					v.setBackgroundColor(getResources().getColor(R.color.touch));  
				}
				return false;     
			}
		});

	}

	private void iniContent() {
		LinearLayout content_layout = (LinearLayout) findViewById(R.id.content_layout);
		View state = View.inflate(this, R.layout.layout_state, null);
		content_layout.addView(state);
		View picture = View.inflate(this, R.layout.layout_picture, null);
		content_layout.addView(picture);
		View passage = View.inflate(this, R.layout.layout_passage, null);
		content_layout.addView(passage);
		View share_state = View.inflate(this, R.layout.layout_share_state, null);
		content_layout.addView(share_state);
		View share_picture = View.inflate(this, R.layout.layout_share_picture, null);
		content_layout.addView(share_picture);
		View share_passage = View.inflate(this, R.layout.layout_share_passage, null);
		content_layout.addView(share_passage);
	}
	
}
